package com.mio.scalademo


import scala.io.StdIn

class User {
  var name: String = null
  var score: Int = 0
  var winNum: Int =0
  var loseNum: Int =0
  var eqNum: Int=0

  def showFist(): Int = {
  	println("请出拳！1.剪刀 2.石头 3.布")
  	val fg: Int = StdIn.readInt()
  	var realfg: Int= fg

    var fgStr = ""
    if(fg ==2){
    	fgStr = "石头"
    }else if(fg ==3){
    	fgStr = "布"
    }else if(fg ==1){
    	fgStr = "剪刀"
    }else{
      realfg =3
      fgStr = "输入不符合规范默认为布"
    } 
    printf("你出拳 %s ", fgStr)
    println("")
    realfg
  }
}

class Computer {
  var name: String = null
  var score: Int = 0
  var winNum: Int =0
  var loseNum: Int =0
  var eqNum: Int=0
  
  def this(name: String) {
    this()
    this.name = name
  }

  
  def showFist(): Int = {
    printf("%s出拳！", name)
     println("")
    var fg = scala.util.Random.nextInt(3)
    var fgStr = ""
    if(fg ==2)
    	fgStr = "石头"
    else if(fg ==3)
    	fgStr = "布"
    else
      fgStr = "剪刀"
      
    printf("%s出拳:%s ", name,fgStr)
    println("")
    fg
  }
}

object Fist {
  def main(args: Array[String]): Unit = {
    println("------------欢迎进入游戏世界---------------")
    println("************************")
    println("*******猜拳，开始*********")
    println("************************")
    //创建机器人角色
    println("请选择对战角色:(1.刘备 2.关羽 3.张飞)")
    val computertype: Int = StdIn.readInt()
    var computer=new Computer("刘备")
    if (computertype ==1) {
      computer=new Computer("刘备")
    }else if (computertype ==2) {
      computer=new Computer("关羽")
    }else{
      computer=new Computer("张飞")
    }
    printf("你选择了与%s对战", computer.name)
    println("")

    //创建用户
    val user=new User()
    
    //开始游戏
    var fistNum =0;
    println("要开始吗？y/n")
    val isStart: String = StdIn.readLine()
    if(isStart.equals("y"))
      fistNum = compareFist(user ,computer,0 )
    else
    	println("退出游戏")
    
    //结束游戏，统计游戏结果
    println("---------------------------")
    printf("游客VS %s ", computer.name)
    println("")
    printf("对战次数 %s ", fistNum)
    println("")
    
    println("姓名   得分  胜局  和局   负局")
    printf("游客   %s    %s    %s    %s ",user.score,user.winNum,user.eqNum,user.loseNum)
    println("")
    printf("%s   %s    %s    %s    %s ", computer.name,computer.score,computer.winNum,computer.eqNum,computer.loseNum)
    println("")
    println("---------------------------")

  }
  //fistNum 猜拳次数
  def compareFist(user: User,computer: Computer,fistNum: Int): Int = {
    var fistNum2 = fistNum +1
    //你出拳
    var userFist = user.showFist()
    //机器人出拳
    var computerFist = computer.showFist()
    
    //计算结果1.剪刀 2.石头 3.布 猜拳结束算分，平局都加一分，获胜加二分，失败不加分
    if(userFist == computerFist){
      
      //平局
      println("结果：和局！下次继续努力！")
      user.score += 1
      user.eqNum += 1
      computer.score += 1
      computer.eqNum += 1
    }else if(userFist==1&&computerFist==3||userFist>computerFist){
       //游客获胜
       println("结果：恭喜，你赢了！")
       user.score += 2
       user.winNum += 1
       computer.loseNum += 1
    }else if(userFist==3&&computerFist==1||userFist<computerFist){  
       //计算机获胜
       println("结果：啊，你输了！")
       computer.score += 2
       computer.winNum += 1
       user.loseNum += 1
    }  
    //是否开始下一轮
    println("是否开始下一轮？y/n")
    val isStart: String = StdIn.readLine()
    if(isStart.equals("y"))
    	compareFist(user ,computer,fistNum2 )
    else
    	println("退出游戏")
    	fistNum
   }
}